<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>SA实战 ·《SpringCloud Alibaba实战》服务治理：实现服务的自动注册与发现 | 冰河技术</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/favicon.ico">
    <script charset="utf-8" async="async" src="/js/jquery.min.js"></script>
    <script charset="utf-8" async="async" src="/js/global.js"></script>
    <script charset="utf-8" async="async" src="/js/fingerprint2.min.js"></script>
    <script charset="utf-8" async="async" src="https://v1.cnzz.com/z_stat.php?id=1281063564&amp;web_id=1281063564"></script>
    <script charset="utf-8" async="async" src="https://s9.cnzz.com/z_stat.php?id=1281064551&amp;web_id=1281064551"></script>
    <script>
            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?d091d2fd0231588b1d0f9231e24e3f5e";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();
            </script>
    <meta name="description" content="包含：编程语言，开发技术，分布式，微服务，高并发，高可用，高可扩展，高可维护，JVM技术，MySQL，分布式数据库，分布式事务，云原生，大数据，云计算，渗透技术，各种面试题，面试技巧...">
    <meta property="article:modified_time" content="2022-05-23T11:30:51.000Z">
    <meta property="og:title" content="SA实战 ·《SpringCloud Alibaba实战》服务治理：实现服务的自动注册与发现">
    <meta property="og:type" content="article">
    <meta property="og:url" content="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html">
    <meta name="twitter:title" content="SA实战 ·《SpringCloud Alibaba实战》服务治理：实现服务的自动注册与发现">
    <meta name="twitter:url" content="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="robots" content="all">
    <meta name="author" content="冰河">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <meta name="keywords" content="冰河，冰河技术, 编程语言，开发技术，分布式，微服务，高并发，高可用，高可扩展，高可维护，JVM技术，MySQL，分布式数据库，分布式事务，云原生，大数据，云计算，渗透技术，各种面试题，面试技巧">
    <meta name="apple-mobile-web-app-capable" content="yes">
    
    <link rel="preload" href="/assets/css/0.styles.ab888ebb.css" as="style"><link rel="preload" href="/assets/css/styles.css?v=1653305936337" as="style"><link rel="preload" href="/assets/js/cg-styles.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-app.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-4.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-3.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-295.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-5.js?v=1653305936337" as="script"><link rel="preload" href="/assets/js/cg-6.js?v=1653305936337" as="script">
    <link rel="stylesheet" href="/assets/css/0.styles.ab888ebb.css"><link rel="stylesheet" href="/assets/css/styles.css?v=1653305936337">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">冰河技术</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/md/other/guide-to-reading.html" class="nav-link">
  导读
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="核心技术" class="dropdown-title"><span class="title">核心技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          Java核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/java/basics/2022-04-28-全网最全正则表达式总结.html" class="nav-link">
  Java基础
</a></li><li class="dropdown-subitem"><a href="/md/core/java/advanced/default.html" class="nav-link">
  Java进阶
</a></li><li class="dropdown-subitem"><a href="/md/core/java/senior/default.html" class="nav-link">
  Java高级
</a></li><li class="dropdown-subitem"><a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html" class="nav-link">
  Java8新特性
</a></li></ul></li><li class="dropdown-item"><h4>
          Spring核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/spring/ioc/2022-04-04-001-聊聊Spring注解驱动开发那些事儿.html" class="nav-link">
  IOC核心技术
</a></li><li class="dropdown-subitem"><a href="/md/core/spring/aop/default.html" class="nav-link">
  AOP核心技术
</a></li></ul></li><li class="dropdown-item"><h4>
          JVM核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/jvm/2022-04-18-001-JVM调优的几种场景.html" class="nav-link">
  JVM调优技术
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="性能调优" class="dropdown-title"><span class="title">性能调优</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/performance/jvm/default.html" class="nav-link">
  JVM性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/tomcat/default.html" class="nav-link">
  Tomcat性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/mysql/default.html" class="nav-link">
  MySQL性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/system/default.html" class="nav-link">
  操作系统性能调优
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="并发编程" class="dropdown-title"><span class="title">并发编程</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/concurrent/bottom/default.html" class="nav-link">
  底层技术
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/source/2020-03-30-001-一文搞懂线程与多线程.html" class="nav-link">
  源码分析
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/basics/2020-03-30-001-明明中断了线程，却为何不起作用呢？.html" class="nav-link">
  基础案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/ActualCombat/default.html" class="nav-link">
  实战案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/interview/default.html" class="nav-link">
  面试
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/framework/default.html" class="nav-link">
  系统架构
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="框架源码" class="dropdown-title"><span class="title">框架源码</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/frame/spring/default.html" class="nav-link">
  Spring源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/springmvc/default.html" class="nav-link">
  SpringMVC源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/mybatis/default.html" class="nav-link">
  MyBatis源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/dubbo/default.html" class="nav-link">
  Dubbo源码
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="分布式" class="dropdown-title"><span class="title">分布式</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          缓存技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/cache/default.html" class="nav-link">
  Redis
</a></li></ul></li><li class="dropdown-item"><h4>
          服务注册发现
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/zookeeper/default.html" class="nav-link">
  Zookeeper
</a></li></ul></li><li class="dropdown-item"><h4>
          消息中间件
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mq/rabbitmq/default.html" class="nav-link">
  RabbitMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/rocketmq/default.html" class="nav-link">
  RocketMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/kafka/default.html" class="nav-link">
  Kafka
</a></li></ul></li><li class="dropdown-item"><h4>
          网络通信
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/netty/default.html" class="nav-link">
  Netty
</a></li></ul></li><li class="dropdown-item"><h4>
          远程调用
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/dubbo/default.html" class="nav-link">
  Dubbo
</a></li></ul></li><li class="dropdown-item"><h4>
          数据库
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mongodb/default.html" class="nav-link">
  MongoDB
</a></li></ul></li><li class="dropdown-item"><h4>
          搜索引擎
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/es/default.html" class="nav-link">
  ElasticSearch
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="微服务" class="dropdown-title"><span class="title">微服务</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springboot/default.html" class="nav-link">
  SpringBoot
</a></li><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloudAlibaba
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><span class="title">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/middleware/bytecode/2022-04-11-001-工作多年的你依然重复做着CRUD-是否接触过这种技术.html" class="nav-link">
  字节码编程
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/threadpool/default.html" class="nav-link">
  手写线程池
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/limiter/default.html" class="nav-link">
  分布式限流
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/independent/default.html" class="nav-link">
  开源项目
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="项目实战" class="dropdown-title"><span class="title">项目实战</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloud Alibaba实战
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="渗透技术" class="dropdown-title"><span class="title">渗透技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/hack/environment/2022-04-17-001-安装Kali系统.html" class="nav-link">
  基础环境篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/tools/2022-04-17-001-使用Easy-Creds工具攻击无线网络.html" class="nav-link">
  渗透工具篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/horse/2022-05-02-001-各种一句话木马大全.html" class="nav-link">
  木马篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sql/2022-05-02-001-sqli-labs-master下载与安装.html" class="nav-link">
  SQL注入篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/shell/2022-05-02-001-各种解析漏洞拿shell.html" class="nav-link">
  漏洞拿Shell篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/crack/2022-05-02-001-使用rarcrack暴力破解RAR-ZIP-7Z压缩包.html" class="nav-link">
  暴力破解篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/bash/2022-05-02-001-3389脚本开启代码(vbs版).html" class="nav-link">
  渗透脚本篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/raising/2022-05-02-001-数据库提权.html" class="nav-link">
  数据与系统提权篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/client/2022-05-02-001-浏览器渗透.html" class="nav-link">
  客户端渗透篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sociology/2022-05-02-001-Metasploit之社会工程学工具包.html" class="nav-link">
  社会工程学
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/question/2022-05-02-001-HTTP错误4031禁止访问-执行访问被拒绝.html" class="nav-link">
  问题记录篇
</a></li></ul></div></div><div class="nav-item"><a href="/md/interview/2022-04-18-001-面试必问-聊聊JVM性能调优.html" class="nav-link">
  面试必问系列
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="📚PDF" class="dropdown-title"><span class="title">📚PDF</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          出版图书
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-深入理解分布式事务.html" class="nav-link">
  《深入理解分布式事务：原理与实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-MySQL技术大全.html" class="nav-link">
  《MySQL技术大全：开发、优化与运维实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-海量数据处理与大数据技术实战.html" class="nav-link">
  《海量数据处理与大数据技术实战》
</a></li></ul></li><li class="dropdown-item"><h4>
          电子书籍
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/pdf/2022-03-30-《冰河的渗透实战笔记》电子书，442页，37万字，正式发布.html" class="nav-link">
  冰河的渗透实战笔记
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关于" class="dropdown-title"><span class="title">关于</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/about/me/about-me.html" class="nav-link">
  关于自己
</a></li><li class="dropdown-item"><!----> <a href="/md/about/study/default.html" class="nav-link">
  关于学习
</a></li><li class="dropdown-item"><!----> <a href="/md/about/job/default.html" class="nav-link">
  关于职场
</a></li></ul></div></div><div class="nav-item"><a href="https://space.bilibili.com/517638832" target="_blank" rel="noopener noreferrer" class="nav-link external">
  B站
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/binghe001/BingheGuide" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/md/other/guide-to-reading.html" class="nav-link">
  导读
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="核心技术" class="dropdown-title"><span class="title">核心技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          Java核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/java/basics/2022-04-28-全网最全正则表达式总结.html" class="nav-link">
  Java基础
</a></li><li class="dropdown-subitem"><a href="/md/core/java/advanced/default.html" class="nav-link">
  Java进阶
</a></li><li class="dropdown-subitem"><a href="/md/core/java/senior/default.html" class="nav-link">
  Java高级
</a></li><li class="dropdown-subitem"><a href="/md/core/java/java8/2022-03-31-001-Java8有哪些新特性呢？.html" class="nav-link">
  Java8新特性
</a></li></ul></li><li class="dropdown-item"><h4>
          Spring核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/spring/ioc/2022-04-04-001-聊聊Spring注解驱动开发那些事儿.html" class="nav-link">
  IOC核心技术
</a></li><li class="dropdown-subitem"><a href="/md/core/spring/aop/default.html" class="nav-link">
  AOP核心技术
</a></li></ul></li><li class="dropdown-item"><h4>
          JVM核心技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/core/jvm/2022-04-18-001-JVM调优的几种场景.html" class="nav-link">
  JVM调优技术
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="性能调优" class="dropdown-title"><span class="title">性能调优</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/performance/jvm/default.html" class="nav-link">
  JVM性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/tomcat/default.html" class="nav-link">
  Tomcat性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/mysql/default.html" class="nav-link">
  MySQL性能调优
</a></li><li class="dropdown-item"><!----> <a href="/md/performance/system/default.html" class="nav-link">
  操作系统性能调优
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="并发编程" class="dropdown-title"><span class="title">并发编程</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/concurrent/bottom/default.html" class="nav-link">
  底层技术
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/source/2020-03-30-001-一文搞懂线程与多线程.html" class="nav-link">
  源码分析
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/basics/2020-03-30-001-明明中断了线程，却为何不起作用呢？.html" class="nav-link">
  基础案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/ActualCombat/default.html" class="nav-link">
  实战案例
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/interview/default.html" class="nav-link">
  面试
</a></li><li class="dropdown-item"><!----> <a href="/md/concurrent/framework/default.html" class="nav-link">
  系统架构
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="框架源码" class="dropdown-title"><span class="title">框架源码</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/frame/spring/default.html" class="nav-link">
  Spring源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/springmvc/default.html" class="nav-link">
  SpringMVC源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/mybatis/default.html" class="nav-link">
  MyBatis源码
</a></li><li class="dropdown-item"><!----> <a href="/md/frame/dubbo/default.html" class="nav-link">
  Dubbo源码
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="分布式" class="dropdown-title"><span class="title">分布式</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          缓存技术
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/cache/default.html" class="nav-link">
  Redis
</a></li></ul></li><li class="dropdown-item"><h4>
          服务注册发现
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/zookeeper/default.html" class="nav-link">
  Zookeeper
</a></li></ul></li><li class="dropdown-item"><h4>
          消息中间件
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mq/rabbitmq/default.html" class="nav-link">
  RabbitMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/rocketmq/default.html" class="nav-link">
  RocketMQ
</a></li><li class="dropdown-subitem"><a href="/md/distributed/mq/kafka/default.html" class="nav-link">
  Kafka
</a></li></ul></li><li class="dropdown-item"><h4>
          网络通信
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/netty/default.html" class="nav-link">
  Netty
</a></li></ul></li><li class="dropdown-item"><h4>
          远程调用
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/dubbo/default.html" class="nav-link">
  Dubbo
</a></li></ul></li><li class="dropdown-item"><h4>
          数据库
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/mongodb/default.html" class="nav-link">
  MongoDB
</a></li></ul></li><li class="dropdown-item"><h4>
          搜索引擎
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/distributed/es/default.html" class="nav-link">
  ElasticSearch
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="微服务" class="dropdown-title"><span class="title">微服务</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springboot/default.html" class="nav-link">
  SpringBoot
</a></li><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloudAlibaba
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="中间件" class="dropdown-title"><span class="title">中间件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/middleware/bytecode/2022-04-11-001-工作多年的你依然重复做着CRUD-是否接触过这种技术.html" class="nav-link">
  字节码编程
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/threadpool/default.html" class="nav-link">
  手写线程池
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/limiter/default.html" class="nav-link">
  分布式限流
</a></li><li class="dropdown-item"><!----> <a href="/md/middleware/independent/default.html" class="nav-link">
  开源项目
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="项目实战" class="dropdown-title"><span class="title">项目实战</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="nav-link">
  SpringCloud Alibaba实战
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="渗透技术" class="dropdown-title"><span class="title">渗透技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/hack/environment/2022-04-17-001-安装Kali系统.html" class="nav-link">
  基础环境篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/tools/2022-04-17-001-使用Easy-Creds工具攻击无线网络.html" class="nav-link">
  渗透工具篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/horse/2022-05-02-001-各种一句话木马大全.html" class="nav-link">
  木马篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sql/2022-05-02-001-sqli-labs-master下载与安装.html" class="nav-link">
  SQL注入篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/shell/2022-05-02-001-各种解析漏洞拿shell.html" class="nav-link">
  漏洞拿Shell篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/crack/2022-05-02-001-使用rarcrack暴力破解RAR-ZIP-7Z压缩包.html" class="nav-link">
  暴力破解篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/bash/2022-05-02-001-3389脚本开启代码(vbs版).html" class="nav-link">
  渗透脚本篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/raising/2022-05-02-001-数据库提权.html" class="nav-link">
  数据与系统提权篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/client/2022-05-02-001-浏览器渗透.html" class="nav-link">
  客户端渗透篇
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/sociology/2022-05-02-001-Metasploit之社会工程学工具包.html" class="nav-link">
  社会工程学
</a></li><li class="dropdown-item"><!----> <a href="/md/hack/question/2022-05-02-001-HTTP错误4031禁止访问-执行访问被拒绝.html" class="nav-link">
  问题记录篇
</a></li></ul></div></div><div class="nav-item"><a href="/md/interview/2022-04-18-001-面试必问-聊聊JVM性能调优.html" class="nav-link">
  面试必问系列
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="📚PDF" class="dropdown-title"><span class="title">📚PDF</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          出版图书
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-深入理解分布式事务.html" class="nav-link">
  《深入理解分布式事务：原理与实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-MySQL技术大全.html" class="nav-link">
  《MySQL技术大全：开发、优化与运维实战》
</a></li><li class="dropdown-subitem"><a href="/md/knowledge/book/2022-03-29-海量数据处理与大数据技术实战.html" class="nav-link">
  《海量数据处理与大数据技术实战》
</a></li></ul></li><li class="dropdown-item"><h4>
          电子书籍
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/md/knowledge/pdf/2022-03-30-《冰河的渗透实战笔记》电子书，442页，37万字，正式发布.html" class="nav-link">
  冰河的渗透实战笔记
</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关于" class="dropdown-title"><span class="title">关于</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/md/about/me/about-me.html" class="nav-link">
  关于自己
</a></li><li class="dropdown-item"><!----> <a href="/md/about/study/default.html" class="nav-link">
  关于学习
</a></li><li class="dropdown-item"><!----> <a href="/md/about/job/default.html" class="nav-link">
  关于职场
</a></li></ul></div></div><div class="nav-item"><a href="https://space.bilibili.com/517638832" target="_blank" rel="noopener noreferrer" class="nav-link external">
  B站
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/binghe001/BingheGuide" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>SpringBoot</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/microservices/springboot/default.html" class="sidebar-link">关于我</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>SpringCloudAlibaba</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/md/microservices/springcloudalibaba/2022-04-02-SpringCloudAlibaba专栏开篇.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》专栏开篇啦</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-04-SA实战·第一篇-专栏设计.html" class="sidebar-link">SA实战 · 《SpringCloud Alibaba实战》专栏设计</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-09-SA实战-微服务介绍.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》微服务介绍</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-13-SA实战·项目说明-流程设计-技术选型-模块划分.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》项目说明、流程设计、技术选型与模块划分</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-18-SA实战-开撸-完成通用模块的开发.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》开撸：完成通用模块的开发</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-21-SA实战-完成三大微服务的搭建与交互开发.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》快速搭建三大微服务并完成交互开发与测试</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA实战-服务治理-实现服务的注册与发现.html" class="active sidebar-link">SA实战 ·《SpringCloud Alibaba实战》服务治理：实现服务的自动注册与发现</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-27-SA实战-第8章-服务治理-实现服务调用的负载均衡.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第8章-服务治理：实现服务调用的负载均衡</a></li><li><a href="/md/microservices/springcloudalibaba/2022-04-28-SA实战-第9章-服务容错-服务雪崩与容错方案.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第9章-服务容错：服务雪崩与容错方案</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-03-SA实战-第10章-服务容错-Fegin整合Sentinel.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第10章-服务容错：项目整合Sentinel实现限流与容错</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-05-SA实战-第11章-服务容错加餐-Sentinel核心技术与配置规则.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第11章-服务容错加餐：Sentinel核心技术与配置规则（最全使用教程）</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-08-SA实战-第12章-服务网关-网关概述与核心架构.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第12章-服务网关：网关概述与核心架构</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-08-SA实战-第13章-服务网关-项目整合SpringCloudGateway.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第13章-服务网关：项目整合SpringCloud Gateway网关</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-10-SA实战-第14章-服务网关-SpringCloudGateway核心技术.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第14章-服务网关加餐：SpringCloud Gateway核心技术</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-11-SA实战-第15章-链路追踪-核心原理与解决方案.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第15章-链路追踪：核心原理与解决方案</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-12-SA实战-第16章-链路追踪-项目整合Sleuth实现链路追踪.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第16章-链路追踪：项目整合Sleuth实现链路追踪</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-13-SA实战-第17章-链路追踪-Sleuth整合ZipKin.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第17章-链路追踪：Sleuth整合ZipKin</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-17-SA实战-第18章-消息服务-MQ使用场景与选型对比.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第18章-消息服务：MQ使用场景与选型对比</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-18-SA实战-第19章-消息服务-项目整合RocketMQ.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第19章-消息服务：项目整合RocketMQ</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-20-SA实战-第20章-消息服务-RocketMQ核心技术.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第20章-消息服务：RocketMQ核心技术</a></li><li><a href="/md/microservices/springcloudalibaba/2022-05-21-SA实战-第21章-服务配置-服务配置介绍与Nacos核心概念.html" class="sidebar-link">SA实战 ·《SpringCloud Alibaba实战》第21章-服务配置：服务配置介绍与Nacos核心概念</a></li></ul></section></li></ul> </aside> <div><main class="page"> <div class="theme-default-content content__default"><h1 id="sa实战-·《springcloud-alibaba实战》服务治理-实现服务的自动注册与发现"><a href="#sa实战-·《springcloud-alibaba实战》服务治理-实现服务的自动注册与发现" class="header-anchor">#</a> SA实战 ·《SpringCloud Alibaba实战》服务治理：实现服务的自动注册与发现</h1> <blockquote><p>注意：本项目完整源码加入 <strong><a href="https://public.zsxq.com/groups/48848484411888.html" target="_blank" rel="noopener noreferrer">冰河技术<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></strong> 知识星球即可获取，文末有优惠券。</p></blockquote> <p><strong>大家好，我是冰河~~</strong></p> <p>在《<a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=Mzg4MjU0OTM1OA==&amp;action=getalbum&amp;album_id=2337104419664084992&amp;scene=173&amp;from_msgid=2247500266&amp;from_itemidx=1&amp;count=3&amp;nolastread=1#wechat_redirect" target="_blank" rel="noopener noreferrer">SpringCloud Alibaba实战<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》专栏的《<a href="https://mp.weixin.qq.com/s?__biz=Mzg4MjU0OTM1OA==&amp;mid=2247500266&amp;idx=1&amp;sn=8b1ef5f6090c4b72f2ee77cda87a0c9d&amp;chksm=cf5676ebf821fffd59555bd51dba6aaf5c09f41b74f7d089136e1600d00c8a475e648598f1f0&amp;scene=178&amp;cur_album_id=2337104419664084992#rd" target="_blank" rel="noopener noreferrer">SA实战 ·《SpringCloud Alibaba实战》快速搭建三大微服务并完成交互开发与测试<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》一文中，我们初步实现了用户微服务、商品微服务和订单微服务之间的交互，但是在实现的过程中，存在一个很明显的问题：那就是将用户微服务所在的IP和端口，以及商品微服务所在的IP和端口硬编码到订单微服务的代码中了。这样的做法存在着非常多的问题。</p> <h2 id="硬编码的问题"><a href="#硬编码的问题" class="header-anchor">#</a> 硬编码的问题</h2> <p>如果将用户微服务和商品微服务所在的IP地址和端口号硬编码到订单微服务中，会存在非常多的问题，其中，最明显的问题有三个，如下所示。</p> <p>（1）如果用户微服务和商品微服务的IP地址或者端口号发生了变化，则订单微服务将变得不可用，需要对同步修改订单微服务中调用用户微服务和商品微服务的IP地址和端口号。</p> <p>（2）如果系统中提供了多个用户微服务和商品微服务，则无法实现微服务的负载均衡功能。</p> <p>（3）如果系统需要支持更高的并发，需要部署更多的用户微服务和商品微服务以及订单微服务，如果将用户微服务和商品微服务的IP地址和端口硬编码到订单微服务，则后续的维护会变得异常复杂。</p> <p>所以，在微服务开发的过程中，需要引入服务治理功能，实现微服务之间的动态注册与发现。</p> <h2 id="服务治理"><a href="#服务治理" class="header-anchor">#</a> 服务治理</h2> <p>如果系统采用了微服务的架构模式，随着微服务数量的不断增多，服务之间的调用关系会变得纵横交错，以纯人工手动的方式来管理这些微服务以及微服务之间的调用关系是及其复杂的，也是极度不可取的。所以，需要引入服务治理的功能。服务治理也是在微服务架构模式下的一种最核心和最基本的模块，主要用来实现各个微服务的自动注册与发现。</p> <p>引入服务治理后，微服务项目总体上可以分为三个大的模块：服务提供者、服务消费者和注册中心，三者的关系如下图所示。</p> <p><img alt="sa-2022-04-25-001" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-04-25-001.png" loading="lazy" class="lazy"></p> <p>（1）服务提供者会将自身提供的服务注册到注册中心，并向注册中心发送心跳信息来证明自己还存活，其中，心跳信息中就会包含服务提供者自身提供的服务信息。</p> <p>（2）注册中心会存储服务提供者上报的信息，并通过服务提供者发送的心跳来更新服务提供者最后的存活时间，如果超过一段时间没有收到服务提供者上报的心跳信息，则注册中心会认为服务提供者不可用，会将对应的服务提供者从服务列表中剔除。</p> <p>（3）服务消费者会向注册中心订阅自身监听的服务，注册中心会保存服务消费者的信息，也会向服务消费者推送服务提供者的信息。</p> <p>（4）服务消费者从注册中心获取到服务提供者的信息时，会直接调用服务提供者的接口来实现远程调用。</p> <p><strong>这里需要注意的是：服务消费者一般会从注册中心中获取到所有服务提供者的信息，根据具体情况实现对具体服务提供者的实例进行访问。</strong></p> <h2 id="注册中心"><a href="#注册中心" class="header-anchor">#</a> 注册中心</h2> <p>从上面的分析可以看出，微服务实现服务治理的关键就是引入了注册中心，它是微服务架构模式下一个非常重要的组件，主要实现了服务注册与发现，服务配置和服务的健康检测等功能。</p> <h3 id="服务注册与发现"><a href="#服务注册与发现" class="header-anchor">#</a> 服务注册与发现</h3> <p>（1）服务注册：注册中心提供保存服务提供者和服务消费者的相关信息。</p> <p>（2）服务发现：也可以理解为服务订阅，服务调用者也就是服务消费者，向注册中心订阅服务提供者的信息，注册中心会向服务消费者推送服务提供者的信息。</p> <h3 id="服务配置"><a href="#服务配置" class="header-anchor">#</a> 服务配置</h3> <p>（1）配置订阅：服务的提供者和消费者都可以向注册中心订阅微服务相关的配置信息。</p> <p>（2）配置下发：注册中心能够将微服务相关的配置信息主动推送给服务的提供者和消费者。</p> <h3 id="服务健康检测"><a href="#服务健康检测" class="header-anchor">#</a> 服务健康检测</h3> <p>注册中心会定期检测存储的服务列表中服务提供者的健康状况，例如服务提供者超过一定的时间没有上报心跳信息，则注册中心会认为对应的服务提供者不可用，就会将服务提供者踢出服务列表。</p> <h3 id="常见的注册中心"><a href="#常见的注册中心" class="header-anchor">#</a> 常见的注册中心</h3> <p>能够实现注册中心功能的组件有很多，但是常用的组件大概包含：Zookeeper、Eureka、Consul、Etcd、Nacos等。这里，就给大家简单介绍下这些能够实现注册中心功能的框架或组件。</p> <p><strong>（1）Zookeeper</strong></p> <p>接触过分布式或者大数据开发的小伙伴应该都知道，Zookeeper是Apache Hadoop的一个子项目，它是一个分布式服务治理框架，主要用来解决应用开发中遇到的一些数据管理问题，例如：分布式集群管理、元数据管理、分布式配置管理、状态同步和统一命名管理等。在高并发环境下，也可以通过Zookeeper实现分布式锁功能。</p> <p><strong>（2）Eureka</strong></p> <p>Eureka是Netflix开源的SpringCloud中支持服务注册与发现的组件，但是后来闭源了。</p> <p><strong>（3）Consul</strong></p> <p>Consul 是 HashiCorp 公司推出的开源产品，用于实现分布式系统的服务发现、服务隔离、服务配置，这些功能中的每一个都可以根据需要单独使用，也可以同时使用所有功能。</p> <p><strong>（4）Etcd</strong></p> <p>etcd 是一个高度一致的分布式键值存储，它提供了一种可靠的方式来存储需要由分布式系统或机器集群访问的数据。它可以优雅地处理网络分区期间的领导者选举，即使在领导者节点中也可以容忍机器故障。</p> <p><strong>（5）Nacos</strong></p> <p>这里，我们重点说下Nacos。Nacos是阿里巴巴开源的一款更易于构建云原生应用的支持动态服务发现、配置管理和服务管理的平台，其提供了一组简单易用的特性集，能够快速实现动态服务发现、服务配置、服务元数据及流量管理，主要如下所示。</p> <ul><li>服务注册：Nacos Client会通过发送REST请求的方式向Nacos Server注册自己的服务，提供自身的元数据，比如IP地址、端口等信
息。Nacos Server接收到注册请求后，就会把这些元数据信息存储在一个双层的内存Map中。</li> <li>服务心跳：在服务注册后，Nacos Client会维护一个定时心跳来持续通知Nacos Server，说明服务一直处于可用状态，防止被剔除。默认5s发送一次心跳。</li> <li>服务健康检查：Nacos Server会开启一个定时任务用来检查注册服务实例的健康情况，对于超过15s没有收到客户端心跳的实例会将它的healthy属性置为false(客户端服务发现时不会发现)，如果某个实例超过30秒没有收到心跳，直接剔除该实例(被剔除的实例如果恢复发送心跳则会重新注册)</li> <li>服务发现：服务消费者（Nacos Client）在调用服务提供者的服务时，会发送一个REST请求给Nacos Server，获取上面注册的服务清
单，并且缓存在Nacos Client本地，同时会在Nacos Client本地开启一个定时任务定时拉取服务端最新的注册表信息更新到本地存。</li> <li>服务同步：Nacos Server集群之间会互相同步服务实例，用来保证服务信息的一致性。</li></ul> <p>这里，我们选用的注册中心就是阿里巴巴开源的Nacos。</p> <h2 id="搭建nacos环境"><a href="#搭建nacos环境" class="header-anchor">#</a> 搭建Nacos环境</h2> <p>（1）到 <a href="https://github.com/alibaba/nacos/releases" target="_blank" rel="noopener noreferrer">https://github.com/alibaba/nacos/releases<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 链接下载Nacos的安装包，我这里下载的安装包为：nacos-server-1.4.3.zip。</p> <p>（2）解压Nacos安装包，并在命令行进入到Nacos的bin目录下执行如下命令以单机的方式启动Nacos。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>startup.cmd -m standalone
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>注意：如果需要以单机的方式启动Nacos，则需要添加 -m standalone 参数，否则，Nacos会以集群的方式启动。</strong></p> <p>（3）启动Nacos之后，在浏览器中输入链接<code>http://localhost:8848/nacos</code> 来访问Nacos的管理界面，默认的用户名和密码都是Nacos，如下所示。</p> <p><img alt="sa-2022-04-25-002" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-04-25-002.png" loading="lazy" class="lazy"></p> <p>输入用户名和密码进入Nacos的管理界面，如下所示。</p> <p><img alt="sa-2022-04-25-003" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-04-25-003.png" loading="lazy" class="lazy"></p> <p>这里，我们进入到Nacos的服务管理-服务列表菜单下，如下所示。</p> <p><img alt="sa-2022-04-25-004" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-04-25-004.png" loading="lazy" class="lazy"></p> <p>可以看到，在Nacos的服务管理-服务列表菜单下还没有任何服务，接下来，我们就对项目的代码进行改造。</p> <h2 id="集成nacos注册中心"><a href="#集成nacos注册中心" class="header-anchor">#</a> 集成Nacos注册中心</h2> <p>引入Nacos注册中心时，我们需要对项目的代码进行一定的改造，以便利用Nacos实现服务的注册与发现功能。</p> <h3 id="改造用户微服务"><a href="#改造用户微服务" class="header-anchor">#</a> 改造用户微服务</h3> <p>（1）在用户微服务的pom.xml文件中添加nacos的服务注册与发现依赖，如下所示。</p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>（2）在用户微服务的resources目录下的application.yml文件中添加Nacos注册中心的服务地址配置，如下所示。</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span>
      <span class="token key atrule">discovery</span><span class="token punctuation">:</span>
        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> 127.0.0.1<span class="token punctuation">:</span><span class="token number">8848</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>（3）在用户微服务的启动类io.binghe.shop#UserStarter上标注@EnableDiscoveryClient注解，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * @author binghe
 * @version 1.0.0
 * @description 启动用户服的类
 */</span>
<span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token annotation punctuation">@EnableTransactionManagement</span><span class="token punctuation">(</span>proxyTargetClass <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@MapperScan</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">&quot;io.binghe.shop.user.mapper&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@EnableDiscoveryClient</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserStarter</span> <span class="token punctuation">{</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">UserStarter</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>此时，就完成了对用户微服务的代码改造。</p> <p>（4）启动用户微服务，并刷新Nacos页面，如下所示。</p> <p><img alt="sa-2022-04-25-005" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-04-25-005.png" loading="lazy" class="lazy"></p> <p>可以看到，用户微服务已经成功注册到Nacos中。</p> <h3 id="改造其他微服务"><a href="#改造其他微服务" class="header-anchor">#</a> 改造其他微服务</h3> <p>我们可以用同样的方式来改造商品微服务和订单微服务的代码，改造好之后，分别启动商品微服务和订单微服务，并再次刷新Nacos的页面，如下所示。</p> <p><img alt="sa-2022-04-25-006" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-04-25-006.png" loading="lazy" class="lazy"></p> <p>可以看到，用户微服务、商品微服务和订单微服务都已成功注册到Nacos。</p> <h2 id="实现服务发现"><a href="#实现服务发现" class="header-anchor">#</a> 实现服务发现</h2> <p>按照整个项目的执行流程，用户执行下单操作时，订单微服务会调用用户微服务的接口获取用户的基本信息，会调用商品微服务的接口获取商品的基本信息。在订单微服务中校验用户的合法性和校验商品库存是否充足，如果用户合法并且商品库存充足，就会向订单数据表中记录订单信息并调用商品微服务的接口来扣减商品的库存。</p> <p>用户微服务和商品微服务作为服务的提供者，而订单微服务作为服务的消费者，如果要实现服务的发现功能，我们还需要对订单微服务的代码进行改造。将订单微服务中硬编码的用户微服务和商品微服务的IP地址和端口号修改成从Nacos中获取。</p> <p>为了让小伙伴们能够更好的对比修改前和修改后的代码，这里，并没有在订单微服务的 <code>io.binghe.shop.order.service.impl#OrderServiceImpl</code> 类上直接修改，还是将其重命名为 <code>io.binghe.shop.order.service.impl.OrderServiceV1Impl</code> 类，同时，再次将其复制一份并命名为<code>io.binghe.shop.order.service.impl.OrderServiceV2Impl</code>类，在后续的开发过程中，如果涉及到大的代码变动，都会以这种方式进行更新。</p> <h3 id="注入服务发现类"><a href="#注入服务发现类" class="header-anchor">#</a> 注入服务发现类</h3> <p>（1）在<code>io.binghe.shop.order.service.impl.OrderServiceV2Impl</code> 类中首先注入DiscoveryClient类的对象，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> <span class="token class-name">DiscoveryClient</span> discoveryClient<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="创建动态服务地址方法"><a href="#创建动态服务地址方法" class="header-anchor">#</a> 创建动态服务地址方法</h3> <p>在<code>io.binghe.shop.order.service.impl.OrderServiceV2Impl</code> 类中创建一个从Nacos中通过服务名称获取IP和端口号的方法getServiceUrl()，并在getServiceUrl()方法中将IP和端口号拼接成<code>IP:PORT</code>的形式，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">private</span> <span class="token class-name">String</span> <span class="token function">getServiceUrl</span><span class="token punctuation">(</span><span class="token class-name">String</span> serviceName<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">ServiceInstance</span> serviceInstance <span class="token operator">=</span> discoveryClient<span class="token punctuation">.</span><span class="token function">getInstances</span><span class="token punctuation">(</span>serviceName<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> serviceInstance<span class="token punctuation">.</span><span class="token function">getHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;:&quot;</span> <span class="token operator">+</span> serviceInstance<span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>具体的实现方式就是调用DiscoveryClient对象的getInstances()方法，并传入服务的名称，从Nacos注册中心中获取一个ServiceInstance类型的List集合，从List集合中获取第1个元素，也就是从List集合中获取到一个ServiceInstance对象，从ServiceInstance对象中获取到IP地址和端口号，并将其拼接成<code>IP:PORT</code>的形式。</p> <h3 id="定义服务提供者名称"><a href="#定义服务提供者名称" class="header-anchor">#</a> 定义服务提供者名称</h3> <p>在<code>io.binghe.shop.order.service.impl.OrderServiceV2Impl</code> 类中定义两个成员变量userServer和productServer，表示用户微服务和商品微服务的服务名称，并将其分别复制为<code>server-user</code>和<code>server-product</code>。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">private</span> <span class="token class-name">String</span> userServer <span class="token operator">=</span> <span class="token string">&quot;server-user&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">private</span> <span class="token class-name">String</span> productServer <span class="token operator">=</span> <span class="token string">&quot;server-product&quot;</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>注意：userServer的值需要与用户微服务下的application.yml文件中的如下配置的值相同。</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> server<span class="token punctuation">-</span>user
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>productServer的值需要与商品微服务下的application.yml文件中的如下配置的值相同。</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> server<span class="token punctuation">-</span>product
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="修改提交订单逻辑"><a href="#修改提交订单逻辑" class="header-anchor">#</a> 修改提交订单逻辑</h3> <p>在<code>io.binghe.shop.order.service.impl.OrderServiceV2Impl</code> 类的saveOrder()方法中，将硬编码的用户微服务和商品微服务的IP和端口修改成从Nacos注册中心中获取，涉及改动的代码片段如下所示。</p> <p>（1）添加获取用户微服务与商品微服务的IP和端口号的代码片段，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">//从Nacos服务中获取用户服务与商品服务的地址</span>
<span class="token class-name">String</span> userUrl <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getServiceUrl</span><span class="token punctuation">(</span>userServer<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> productUrl <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getServiceUrl</span><span class="token punctuation">(</span>productServer<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>（2）修改使用restTemplate获取用户信息的代码片段，修改前的代码片段如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">User</span> user <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span><span class="token string">&quot;http://localhost:8060/user/get/&quot;</span> <span class="token operator">+</span> orderParams<span class="token punctuation">.</span><span class="token function">getUserId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">User</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>user <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token string">&quot;未获取到用户信息: &quot;</span> <span class="token operator">+</span> <span class="token class-name">JSONObject</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>orderParams<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>修改后的代码片段如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">User</span> user <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span><span class="token string">&quot;http://&quot;</span> <span class="token operator">+</span> userUrl <span class="token operator">+</span> <span class="token string">&quot;/user/get/&quot;</span> <span class="token operator">+</span> orderParams<span class="token punctuation">.</span><span class="token function">getUserId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">User</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>user <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token string">&quot;未获取到用户信息: &quot;</span> <span class="token operator">+</span> <span class="token class-name">JSONObject</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>orderParams<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>可以看到，订单微服务获取用户微服务信息时，不再是硬编码用户微服务的IP地址和端口号了。</p> <p>（3）修改使用restTemplate获取商品信息的代码片段，修改前的代码片段如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Product</span> product <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span><span class="token string">&quot;http://localhost:8070/product/get/&quot;</span> <span class="token operator">+</span> orderParams<span class="token punctuation">.</span><span class="token function">getProductId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">Product</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>product <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token string">&quot;未获取到商品信息: &quot;</span> <span class="token operator">+</span> <span class="token class-name">JSONObject</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>orderParams<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>修改后的代码片段如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Product</span> product <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span><span class="token string">&quot;http://&quot;</span> <span class="token operator">+</span> productUrl <span class="token operator">+</span> <span class="token string">&quot;/product/get/&quot;</span> <span class="token operator">+</span> orderParams<span class="token punctuation">.</span><span class="token function">getProductId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">Product</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>product <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token string">&quot;未获取到商品信息: &quot;</span> <span class="token operator">+</span> <span class="token class-name">JSONObject</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>orderParams<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>可以看到，订单微服务获取商品微服务信息时，不再是硬编码商品微服务的IP地址和端口号了。</p> <p>（4）修改使用restTemplate扣减商品库存的代码片段，修改前的代码片段如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> result <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span><span class="token string">&quot;http://localhost:8070/product/update_count/&quot;</span> <span class="token operator">+</span> orderParams<span class="token punctuation">.</span><span class="token function">getProductId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;/&quot;</span> <span class="token operator">+</span> orderParams<span class="token punctuation">.</span><span class="token function">getCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>result<span class="token punctuation">.</span><span class="token function">getCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token class-name">HttpCode</span><span class="token punctuation">.</span>SUCCESS<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token string">&quot;库存扣减失败&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>修改后的代码片段如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Result</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> result <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span><span class="token string">&quot;http://&quot;</span> <span class="token operator">+</span> productUrl <span class="token operator">+</span> <span class="token string">&quot;/product/update_count/&quot;</span> <span class="token operator">+</span> orderParams<span class="token punctuation">.</span><span class="token function">getProductId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;/&quot;</span> <span class="token operator">+</span> orderParams<span class="token punctuation">.</span><span class="token function">getCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">Result</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>result<span class="token punctuation">.</span><span class="token function">getCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token class-name">HttpCode</span><span class="token punctuation">.</span>SUCCESS<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token string">&quot;库存扣减失败&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>可以看到，订单微服务调用商品微服务的扣减商品库存接口时，不再是硬编码商品微服务的IP地址和端口号了。</p> <p>注意：修改后的<code>io.binghe.shop.order.service.impl.OrderServiceV2Impl</code> 类的完整源码，小伙伴们可自行查看项目代码，冰河在这里不再赘述。</p> <p>至此，整个项目就改造完成了。接下来，我们进行测试。</p> <h2 id="测试项目"><a href="#测试项目" class="header-anchor">#</a> 测试项目</h2> <p>开发完成后，我们对快速搭建并开发完成的三大微服务进行简单的测试，在测试之前我们需要先在数据表中添加一些测试数据。</p> <h3 id="添加测试数据"><a href="#添加测试数据" class="header-anchor">#</a> 添加测试数据</h3> <p>（1）在用户表中添加一条id为1001的记录，如下所示。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token identifier"><span class="token punctuation">`</span>shop<span class="token punctuation">`</span></span><span class="token punctuation">.</span><span class="token identifier"><span class="token punctuation">`</span>t_user<span class="token punctuation">`</span></span><span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_username<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_password<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_phone<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_address<span class="token punctuation">`</span></span><span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">1001</span><span class="token punctuation">,</span> <span class="token string">'binghe'</span><span class="token punctuation">,</span> <span class="token string">'c26be8aaf53b15054896983b43eb6a65'</span><span class="token punctuation">,</span> <span class="token string">'13212345678'</span><span class="token punctuation">,</span> <span class="token string">'北京'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>（2）在商品数据表中添加几条商品记录，如下所示。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token identifier"><span class="token punctuation">`</span>shop<span class="token punctuation">`</span></span><span class="token punctuation">.</span><span class="token identifier"><span class="token punctuation">`</span>t_product<span class="token punctuation">`</span></span><span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_pro_name<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_pro_price<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_pro_stock<span class="token punctuation">`</span></span><span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">1001</span><span class="token punctuation">,</span> <span class="token string">'华为'</span><span class="token punctuation">,</span> <span class="token number">2399.00</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token identifier"><span class="token punctuation">`</span>shop<span class="token punctuation">`</span></span><span class="token punctuation">.</span><span class="token identifier"><span class="token punctuation">`</span>t_product<span class="token punctuation">`</span></span><span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_pro_name<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_pro_price<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_pro_stock<span class="token punctuation">`</span></span><span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">1002</span><span class="token punctuation">,</span> <span class="token string">'小米'</span><span class="token punctuation">,</span> <span class="token number">1999.00</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token identifier"><span class="token punctuation">`</span>shop<span class="token punctuation">`</span></span><span class="token punctuation">.</span><span class="token identifier"><span class="token punctuation">`</span>t_product<span class="token punctuation">`</span></span><span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_pro_name<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_pro_price<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>t_pro_stock<span class="token punctuation">`</span></span><span class="token punctuation">)</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">1003</span><span class="token punctuation">,</span> <span class="token string">'iphone'</span><span class="token punctuation">,</span> <span class="token number">4999.00</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="测试库存不足的情况"><a href="#测试库存不足的情况" class="header-anchor">#</a> 测试库存不足的情况</h3> <p>（1）分别启动用户微服务、商品微服务和订单微服务。</p> <p>（2）查询id为1001的商品信息，如下所示。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> t_product <span class="token keyword">where</span> id <span class="token operator">=</span> <span class="token number">1001</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------+------------+-------------+-------------+</span>
<span class="token operator">|</span> id   <span class="token operator">|</span> t_pro_name <span class="token operator">|</span> t_pro_price <span class="token operator">|</span> t_pro_stock <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------------+-------------+-------------+</span>
<span class="token operator">|</span> <span class="token number">1001</span> <span class="token operator">|</span> 华为       <span class="token operator">|</span>     <span class="token number">2399.00</span> <span class="token operator">|</span>         <span class="token number">100</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------------+-------------+-------------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>可以看到，id为1001的商品的库存为100。</p> <p>（3）查询订单表和订单条目表中的数据，如下所示。</p> <ul><li>查询订单表</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> t_order<span class="token punctuation">;</span>
Empty <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>可以看到，订单数据表的数据为空。</p> <ul><li>查询订单条目表</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> t_order_item<span class="token punctuation">;</span>
Empty <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>可以看到，订单条目数据表的数据为空。</p> <p>（4）在浏览器中调用订单微服务的下单接口，传入的商品数量为1001，如下所示。</p> <p><img alt="sa-2022-04-21-005" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-04-21-005.png" loading="lazy" class="lazy"></p> <p>可以看到，返回的信息中，code为500，codeMsg输出的信息为执行失败，data返回的结果为商品库存不足，并且输出了提交的参数信息。</p> <p>（5）再次查询id为1001的商品信息，如下所示。</p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> t_product <span class="token keyword">where</span> id <span class="token operator">=</span> <span class="token number">1001</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------+------------+-------------+-------------+</span>
<span class="token operator">|</span> id   <span class="token operator">|</span> t_pro_name <span class="token operator">|</span> t_pro_price <span class="token operator">|</span> t_pro_stock <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------------+-------------+-------------+</span>
<span class="token operator">|</span> <span class="token number">1001</span> <span class="token operator">|</span> 华为       <span class="token operator">|</span>     <span class="token number">2399.00</span> <span class="token operator">|</span>         <span class="token number">100</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------+------------+-------------+-------------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>可以看到，商品id为1001的商品库存仍为100，并没有减少。</p> <p>（6）再次查询订单表和订单条目表中的数据，如下所示。</p> <ul><li>查询订单表</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> t_order<span class="token punctuation">;</span>
Empty <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>可以看到，订单数据表的数据为空。</p> <ul><li>查询订单条目表</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> t_order_item<span class="token punctuation">;</span>
Empty <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>可以看到，订单条目数据表的数据为空。</p> <p><strong>综上，当提交订单时传入的商品数量大于商品的库存数量时，系统会抛出异常，并不会执行提交订单和扣减库存的操作。</strong></p> <h3 id="测试正常下单的情况"><a href="#测试正常下单的情况" class="header-anchor">#</a> 测试正常下单的情况</h3> <p>（1）在测试库存不足的情况的基础上，我们将调用提交订单的接口时传入的商品数量修改为10，如下所示。</p> <p><img alt="sa-2022-04-21-006" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-04-21-006.png" loading="lazy" class="lazy"></p> <p>可以看到，当商品库存充足时，调用订单微服务的下单接口，返回的数据为success表示下单成功。</p> <p>（2）再次查询id为1001的商品信息，如下所示。</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>mysql<span class="token operator">&gt;</span> select <span class="token operator">*</span> from t_product where id <span class="token operator">=</span> <span class="token number">1001</span><span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span>
<span class="token operator">|</span> id   <span class="token operator">|</span> t_pro_name <span class="token operator">|</span> t_pro_price <span class="token operator">|</span> t_pro_stock <span class="token operator">|</span>
<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span>
<span class="token operator">|</span> <span class="token number">1001</span> <span class="token operator">|</span> 华为       <span class="token operator">|</span>     <span class="token number">2399.00</span> <span class="token operator">|</span>          <span class="token number">90</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span>
<span class="token number">1</span> row in set <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>可以看到，id为1001的商品库存由原来的100变更为90，减少了10个库存。</p> <p>（3）再次查询订单表和订单条目表中的数据，如下所示。</p> <ul><li>查询订单表</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> t_order<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------------------+-----------+-------------+-------------+-----------+---------------+</span>
<span class="token operator">|</span> id               <span class="token operator">|</span> t_user_id <span class="token operator">|</span> t_user_name <span class="token operator">|</span> t_phone     <span class="token operator">|</span> t_address <span class="token operator">|</span> t_total_price <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------------------+-----------+-------------+-------------+-----------+---------------+</span>
<span class="token operator">|</span> <span class="token number">3270016896208896</span> <span class="token operator">|</span>      <span class="token number">1001</span> <span class="token operator">|</span> binghe      <span class="token operator">|</span> <span class="token number">13212345678</span> <span class="token operator">|</span> 北京      <span class="token operator">|</span>      <span class="token number">23990.00</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------------------+-----------+-------------+-------------+-----------+---------------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>可以看到，订单数据表中成功记录了订单的信息</p> <ul><li>查询订单条目表</li></ul> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code>mysql<span class="token operator">&gt;</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> t_order_item<span class="token punctuation">;</span>
<span class="token operator">+</span><span class="token comment">------------------+------------------+----------+------------+-------------+----------+</span>
<span class="token operator">|</span> id               <span class="token operator">|</span> t_order_id       <span class="token operator">|</span> t_pro_id <span class="token operator">|</span> t_pro_name <span class="token operator">|</span> t_pro_price <span class="token operator">|</span> t_number <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------------------+------------------+----------+------------+-------------+----------+</span>
<span class="token operator">|</span> <span class="token number">3270017277890560</span> <span class="token operator">|</span> <span class="token number">3270016896208896</span> <span class="token operator">|</span>     <span class="token number">1001</span> <span class="token operator">|</span> 华为       <span class="token operator">|</span>     <span class="token number">2399.00</span> <span class="token operator">|</span>       <span class="token number">10</span> <span class="token operator">|</span>
<span class="token operator">+</span><span class="token comment">------------------+------------------+----------+------------+-------------+----------+</span>
<span class="token number">1</span> <span class="token keyword">row</span> <span class="token operator">in</span> <span class="token keyword">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>可以看到，订单条目数据表中成功记录了订单条目的信息。</p> <p>至此，项目的测试完毕。</p> <p>另外，小伙伴们在【冰河技术】知识星球获取到源码后，可以自行测试其他异常情况，比如商品不存在的异常和用户不存在的异常，或者自行验证几个其他的异常情况，看提交是否提交，商品库存是否扣减了。</p> <p>这次，我们只是使用SpringBoot快速搭建了三个微服务项目，从下一篇开始，我们就要逐渐接入SpringCloud Alibaba技术了，小伙伴们，你们准备好了吗？加入【冰河技术】知识星球，一起搞定《<a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=Mzg4MjU0OTM1OA==&amp;action=getalbum&amp;album_id=2337104419664084992&amp;scene=173&amp;from_msgid=2247500214&amp;from_itemidx=1&amp;count=3&amp;nolastread=1#wechat_redirect" target="_blank" rel="noopener noreferrer">SpringCloud Alibaba实战<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》吧，加油！！</p> <h2 id="关于星球"><a href="#关于星球" class="header-anchor">#</a> 关于星球</h2> <p>最近，冰河创建了【冰河技术】知识星球，《<a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=Mzg4MjU0OTM1OA==&amp;action=getalbum&amp;album_id=2337104419664084992&amp;scene=173&amp;from_msgid=2247500214&amp;from_itemidx=1&amp;count=3&amp;nolastread=1#wechat_redirect" target="_blank" rel="noopener noreferrer">SpringCloud Alibaba实战<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>》专栏的源码获取方式会放到知识星期中，同时在微信上会创建专门的知识星球群，冰河会在知识星球上和星球群里解答球友的提问。</p> <h3 id="星球提供的服务"><a href="#星球提供的服务" class="header-anchor">#</a> 星球提供的服务</h3> <p>冰河整理了星球提供的一些服务，如下所示。</p> <p>加入星球，你将获得：</p> <p>1.学习SpringCloud Alibaba实战项目—从零开发微服务项目</p> <p>2.学习高并发、大流量业务场景的解决方案，体验大厂真正的高并发、大流量的业务场景</p> <p>3.学习进大厂必备技能：性能调优、并发编程、分布式、微服务、框架源码、中间件开发、项目实战</p> <p>4.提供站点 https://binghe001.github.io 所有学习内容的指导、帮助</p> <p>5.GitHub：https://github.com/binghe001/BingheGuide - 非常有价值的技术资料仓库，包括冰河所有的博客开放案例代码</p> <p>6.可以发送你的简历到我的邮箱，提供简历批阅服务</p> <p>7.提供技术问题、系统架构、学习成长、晋升答辩等各项内容的回答</p> <p>8.定期的整理和分享出各类专属星球的技术小册、电子书、编程视频、PDF文件</p> <p>9.定期组织技术直播分享，传道、授业、解惑，指导阶段瓶颈突破技巧</p> <h3 id="星球门票价格"><a href="#星球门票价格" class="header-anchor">#</a> 星球门票价格</h3> <p>星球目前的门票价格50元，随着每次加入新实战项目和分享硬核技术上调入场价格。</p> <p>最后，小伙伴们可以扫描或者长按下图中的二维码加入星球，也可以在 冰河技术 公众号回复“星球”，领取入场优惠券。</p> <p><img alt="sa-2022-04-21-007" data-src="https://binghe001.github.io/assets/images/microservices/springcloudalibaba/sa-2022-04-21-007.png" loading="lazy" class="lazy"></p> <p><strong>好了，今天就到这儿吧，我是冰河，我们下期见~~</strong></p> <h2 id="写在最后"><a href="#写在最后" class="header-anchor">#</a> 写在最后</h2> <blockquote><p>如果你觉得冰河写的还不错，请微信搜索并关注「 <strong>冰河技术</strong> 」微信公众号，跟冰河学习高并发、分布式、微服务、大数据、互联网和云原生技术，「 <strong>冰河技术</strong> 」微信公众号更新了大量技术专题，每一篇技术文章干货满满！不少读者已经通过阅读「 <strong>冰河技术</strong> 」微信公众号文章，吊打面试官，成功跳槽到大厂；也有不少读者实现了技术上的飞跃，成为公司的技术骨干！如果你也想像他们一样提升自己的能力，实现技术能力的飞跃，进大厂，升职加薪，那就关注「 <strong>冰河技术</strong> 」微信公众号吧，每天更新超硬核技术干货，让你对如何提升技术能力不再迷茫！</p></blockquote> <p><img alt="" data-src="https://img-blog.csdnimg.cn/20200906013715889.png" loading="lazy" class="lazy"></p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/binghe001/BingheGuide/edit/master/docs/md/microservices/springcloudalibaba/2022-04-25-SA实战-服务治理-实现服务的注册与发现.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">2022/5/23</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/md/microservices/springcloudalibaba/2022-04-21-SA实战-完成三大微服务的搭建与交互开发.html" class="prev">
          SA实战 ·《SpringCloud Alibaba实战》快速搭建三大微服务并完成交互开发与测试
        </a></span> <span class="next"><a href="/md/microservices/springcloudalibaba/2022-04-27-SA实战-第8章-服务治理-实现服务调用的负载均衡.html">
          SA实战 ·《SpringCloud Alibaba实战》第8章-服务治理：实现服务调用的负载均衡
        </a>
        →
      </span></p></div> </main></div> <aside class="page-sidebar"> <div class="page-side-toolbar"><div class="option-box-toc-fixed"><div class="toc-container-sidebar"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="max-height:650px"><div style="font-weight:bold;text-align:center;">SA实战 ·《SpringCloud Alibaba实战》服务治理：实现服务的自动注册与发现</div> <hr> <div class="toc-box"><ul class="toc-sidebar-links"><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#硬编码的问题" class="toc-sidebar-link">硬编码的问题</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#服务治理" class="toc-sidebar-link">服务治理</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#注册中心" class="toc-sidebar-link">注册中心</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#服务注册与发现" class="toc-sidebar-link">服务注册与发现</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#服务配置" class="toc-sidebar-link">服务配置</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#服务健康检测" class="toc-sidebar-link">服务健康检测</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#常见的注册中心" class="toc-sidebar-link">常见的注册中心</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#搭建nacos环境" class="toc-sidebar-link">搭建Nacos环境</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#集成nacos注册中心" class="toc-sidebar-link">集成Nacos注册中心</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#改造用户微服务" class="toc-sidebar-link">改造用户微服务</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#改造其他微服务" class="toc-sidebar-link">改造其他微服务</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#实现服务发现" class="toc-sidebar-link">实现服务发现</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#注入服务发现类" class="toc-sidebar-link">注入服务发现类</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#创建动态服务地址方法" class="toc-sidebar-link">创建动态服务地址方法</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#定义服务提供者名称" class="toc-sidebar-link">定义服务提供者名称</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#修改提交订单逻辑" class="toc-sidebar-link">修改提交订单逻辑</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#测试项目" class="toc-sidebar-link">测试项目</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#添加测试数据" class="toc-sidebar-link">添加测试数据</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#测试库存不足的情况" class="toc-sidebar-link">测试库存不足的情况</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#测试正常下单的情况" class="toc-sidebar-link">测试正常下单的情况</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#关于星球" class="toc-sidebar-link">关于星球</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#星球提供的服务" class="toc-sidebar-link">星球提供的服务</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#星球门票价格" class="toc-sidebar-link">星球门票价格</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#写在最后" class="toc-sidebar-link">写在最后</a><ul class="toc-sidebar-sub-headers"></ul></li></ul></div></div></div></div></div> <div class="option-box-toc-over"><img src="/images/system/toc.png" class="nozoom"> <span class="show-txt">目录</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="max-height:550px"><div style="font-weight:bold;text-align:center;">SA实战 ·《SpringCloud Alibaba实战》服务治理：实现服务的自动注册与发现</div> <hr> <div class="toc-box"><ul class="toc-sidebar-links"><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#硬编码的问题" class="toc-sidebar-link">硬编码的问题</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#服务治理" class="toc-sidebar-link">服务治理</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#注册中心" class="toc-sidebar-link">注册中心</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#服务注册与发现" class="toc-sidebar-link">服务注册与发现</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#服务配置" class="toc-sidebar-link">服务配置</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#服务健康检测" class="toc-sidebar-link">服务健康检测</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#常见的注册中心" class="toc-sidebar-link">常见的注册中心</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#搭建nacos环境" class="toc-sidebar-link">搭建Nacos环境</a><ul class="toc-sidebar-sub-headers"></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#集成nacos注册中心" class="toc-sidebar-link">集成Nacos注册中心</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#改造用户微服务" class="toc-sidebar-link">改造用户微服务</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#改造其他微服务" class="toc-sidebar-link">改造其他微服务</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#实现服务发现" class="toc-sidebar-link">实现服务发现</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#注入服务发现类" class="toc-sidebar-link">注入服务发现类</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#创建动态服务地址方法" class="toc-sidebar-link">创建动态服务地址方法</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#定义服务提供者名称" class="toc-sidebar-link">定义服务提供者名称</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#修改提交订单逻辑" class="toc-sidebar-link">修改提交订单逻辑</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#测试项目" class="toc-sidebar-link">测试项目</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#添加测试数据" class="toc-sidebar-link">添加测试数据</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#测试库存不足的情况" class="toc-sidebar-link">测试库存不足的情况</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#测试正常下单的情况" class="toc-sidebar-link">测试正常下单的情况</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#关于星球" class="toc-sidebar-link">关于星球</a><ul class="toc-sidebar-sub-headers"><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#星球提供的服务" class="toc-sidebar-link">星球提供的服务</a></li><li class="toc-sidebar-sub-header"><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#星球门票价格" class="toc-sidebar-link">星球门票价格</a></li></ul></li><li><a href="/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html#写在最后" class="toc-sidebar-link">写在最后</a><ul class="toc-sidebar-sub-headers"></ul></li></ul></div></div></div></div></div> <div class="option-box"><img src="/images/system/wechat.png" class="nozoom"> <span class="show-txt">手机看</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.9rem">微信扫一扫</span> <img height="180px" src="https://api.qrserver.com/v1/create-qr-code/?data=https://binghe001.github.io/md/microservices/springcloudalibaba/2022-04-25-SA%E5%AE%9E%E6%88%98-%E6%9C%8D%E5%8A%A1%E6%B2%BB%E7%90%86-%E5%AE%9E%E7%8E%B0%E6%9C%8D%E5%8A%A1%E7%9A%84%E6%B3%A8%E5%86%8C%E4%B8%8E%E5%8F%91%E7%8E%B0.html" style="margin:10px;">
                可以<b>手机看</b>或分享至<b>朋友圈</b></div></div></div></div> <div class="option-box"><img src="/images/system/toggle.png" width="30px" class="nozoom"> <span class="show-txt">左栏</span></div> <div class="option-box"><img src="/images/system/xingqiu.png" width="25px" class="nozoom"> <span class="show-txt">星球</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">实战项目<span style="font-size:8px;color:red;">「SpringCloud Alibaba实战项目」</span>、专属电子书、问题解答、简历指导、技术分享、晋升指导、视频课程</span> <img height="180px" src="/images/personal/xingqiu.png" style="margin:10px;"> <b>知识星球</b>：冰河技术
            </div></div></div></div> <div class="option-box"><img src="/images/system/wexin4.png" width="25px" class="nozoom"> <span class="show-txt">读者群</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">添加冰河微信<span style="color:red;">(hacker_binghe)</span>进冰河技术学习交流圈「无任何套路」</span> <img src="/images/personal/hacker_binghe.jpg" height="180px" style="margin:10px;">
                PS：添加时请备注<b>读者加群</b>，谢谢！
              </div></div></div></div> <div class="option-box"><img src="/images/system/download-2.png" width="25px" class="nozoom"> <span class="show-txt">下资料</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">扫描公众号，回复<span style="color:red;">“1024”</span>下载<span style="color:red;">100GB+</span>学习技术资料、PDF书籍、实战项目、简历模板等「无任何套路」</span> <img src="/images/personal/qrcode.png" height="180px" style="margin:10px;"> <b>公众号:</b> 冰河技术
              </div></div></div></div> <div class="option-box"><img src="/images/system/heart-1.png" width="25px" class="nozoom"> <span class="show-txt">赞赏我</span> <div class="toc-container"><div class="pos-box"><div class="icon-arrow"></div> <div class="scroll-box" style="text-align:center"><span style="font-size:0.8rem;font-weight:bold;">鼓励/支持/赞赏我</span> <img height="180px" src="/images/personal/encourage-head.png" style="margin:5px;"> <br>1. 不靠它生存但仍希望得到你的鼓励；
                <br>2. 时刻警醒自己保持技术人的初心；
              </div></div></div></div> <div title="SA实战 ·《SpringCloud Alibaba实战》快速搭建三大微服务并完成交互开发与测试" class="option-box" style="padding-left:2px;text-align:center;"><a href="/md/microservices/springcloudalibaba/2022-04-21-SA实战-完成三大微服务的搭建与交互开发.html"><img src="/images/system/pre2.png" width="30px" class="nozoom"> <span class="show-txt">上一篇</span></a></div> <div title="SA实战 ·《SpringCloud Alibaba实战》第8章-服务治理：实现服务调用的负载均衡" class="option-box" style="padding-left:2px;text-align:center;"><a href="/md/microservices/springcloudalibaba/2022-04-27-SA实战-第8章-服务治理-实现服务调用的负载均衡.html"><img src="/images/system/next2.png" width="30px" class="nozoom"> <span class="show-txt">下一篇</span></a></div></div>  <!----> </aside></div><div class="global-ui"><div class="read-more-wrap" style="display:none;position:absolute;bottom:0px;z-index:9999;width:100%;margin-top:-100px;font-family:PingFangSC-Regular, sans-serif;"><div id="read-more-mask" style="position: relative; height: 200px; background: -webkit-gradient(linear, 0 0%, 0 100%, from(rgba(255, 255, 255, 0)), to(rgb(255, 255, 255)));"></div> <a id="read-more-btn" target="_self" style="position: absolute; left: 50%; top: 70%; bottom: 30px; transform: translate(-50%, -50%); width: 160px; height: 36px; line-height: 36px; font-size: 15px; text-align: center; border: 1px solid rgb(222, 104, 109); color: rgb(222, 104, 109); background: rgb(255, 255, 255); cursor: pointer; border-radius: 6px;">阅读全文</a> <div id="btw-modal-wrap" style="display: none;"><div id="btw-mask" style="position: fixed; top: 0px; right: 0px; bottom: 0px; left: 0px; opacity: 0.7; z-index: 999; background: rgb(0, 0, 0);"></div> <div id="btw-modal" style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; text-align: center; font-size: 13px; background: rgb(255, 255, 255); border-radius: 10px; z-index: 9999; font-family: PingFangSC-Regular, sans-serif;"><span id="btw-modal-close-btn" style="position: absolute; top: 5px; right: 15px; line-height: 34px; font-size: 34px; cursor: pointer; opacity: 0.2; z-index: 9999; color: rgb(0, 0, 0); background: none; border: none; outline: none;">×</span> <p id="btw-modal-header" style="margin-top: 40px; line-height: 1.8; font-size: 13px;">
                扫码或搜索：<span style="color: #E9405A; font-weight: bold;">冰河技术</span> <br>发送：<span id="fustack-token" class="token" style="color: #e9415a; font-weight: bold; font-size: 17px; margin-bottom: 45px;">290992</span> <br>即可<span style="color: #e9415a; font-weight: bold;">立即永久</span>解锁本站全部文章</p> <img src="/images/personal/qrcode.png" style="width: 180px; margin-top: 10px; margin-bottom: 30px; border: 8px solid rgb(230, 230, 230);"></div></div></div><div class="pay-read-more-wrap" style="display:none;position:absolute;bottom:0px;z-index:9999;width:100%;margin-top:-100px;font-family:PingFangSC-Regular, sans-serif;"><div id="pay-read-more-mask" style="position: relative; height: 200px; background: -webkit-gradient(linear, 0 0%, 0 100%, from(rgba(255, 255, 255, 0)), to(rgb(255, 255, 255)));"></div> <a id="pay-read-more-btn" target="_blank" style="position: absolute; left: 50%; top: 70%; bottom: 30px; transform: translate(-50%, -50%); width: 160px; height: 36px; line-height: 36px; font-size: 15px; text-align: center; border: 1px solid rgb(222, 104, 109); color: rgb(222, 104, 109); background: rgb(255, 255, 255); cursor: pointer; border-radius: 6px;">付费阅读</a></div></div></div>
    <script src="/assets/js/cg-styles.js?v=1653305936337" defer></script><script src="/assets/js/cg-4.js?v=1653305936337" defer></script><script src="/assets/js/cg-3.js?v=1653305936337" defer></script><script src="/assets/js/cg-295.js?v=1653305936337" defer></script><script src="/assets/js/cg-5.js?v=1653305936337" defer></script><script src="/assets/js/cg-6.js?v=1653305936337" defer></script><script src="/assets/js/cg-app.js?v=1653305936337" defer></script>
  </body>
</html>
